package com.rq.adapter;


import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.SparseArray;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.rq.rvlibrary.BaseAdapter;
import com.rq.rvlibrary.BaseViewHolder;
import com.rq.rvlibrary.EasyViewHolder;
import com.rq.rvlibrary.EmptyViewHolder;
import com.rq.rvlibrary.ItemAnimator;
import com.rq.rvlibrary.OnItemClickListener;
import com.rq.rvlibrary.RecyclerUtil;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements OnItemClickListener {
    BaseAdapter mAdapter;
    RecyclerView recyclerView;
    RecyclerView.Adapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.rv);
        //    simpleUse();
//    classUse();
        addUse();
    }

    private void addUse() {
        SparseArray layList = new SparseArray();
        layList.append(R.layout.item_green, EasyViewHolder.class);
        layList.append(R.layout.item_red, ExampleRedViewHolder.class);
        layList.append(R.layout.item_purple, ExamplePurpleViewHolder.class);
        mAdapter = new BaseAdapter(this, layList) {
            @Override
            protected int getMultipleHolderType(Object dataItem, int position) {
                if (dataItem instanceof Integer) {
                    return R.layout.item_green;
                }
                if (dataItem instanceof MoreData) {
                    return R.layout.item_red;
                }
                String data = (String) dataItem;
                if (position < 4 && position >= 0) return R.layout.item_green;
//                if (data.contains("1")) return R.layout.item_purple;
                return R.layout.item_purple;
            }

            @Override
            protected void onBindMultipleHolder(BaseAdapter adapter, BaseViewHolder holder, int position,
                                                Object o) {
                LOG.e("MainActivity", holder.getClass()
                        .getSimpleName() + ".onBindMultipleHolder.63:" + position);
                if (holder instanceof ExamplePurpleViewHolder) {
                    LOG.e("MainActivity", "67:" + o);
                    if (o instanceof Integer) {
                        holder.setItemText(R.id.txt, "Holder2 数字:" + o);
                    } else {
                        holder.setItemText(R.id.txt, "Holder2:" + o);
                    }
                } else if (position >= 0) if (o instanceof Integer) {
                    holder.setItemText(R.id.txt, "数字:" + o);
                } else {
                    holder.setItemText(R.id.txt, String.valueOf(o));
                }
            }
        };
        new RecyclerUtil(mAdapter).set2View(recyclerView);
        List debug = new ArrayList<>();
        debug.addAll(getDebugData());
        debug.add("17---***");
        debug.add(18);
        debug.add(19);
        debug.add("20***");
        debug.add(21);
        debug.add(22);
        debug.add("23333333333");
        debug.add("244444");
        mAdapter.setData(debug);
    }

    private void classUse() {
        mAdapter = new BaseAdapter(this, R.layout.item_red, ExampleRedViewHolder.class);
        mAdapter.addOnItemClickListener(EmptyViewHolder.class, new OnItemClickListener() {
            @Override
            public void onItemClick(BaseAdapter adapter, BaseViewHolder holder, Object o, View view,
                                    int position) {
                Toast.makeText(MainActivity.this, "空数据", Toast.LENGTH_SHORT).show();
            }
        });
        //    当 ExampleViewHolder1 为内部类是使用下个方法
        //    mAdapter = new BaseAdapter(this, R.layout.item_example,ExampleViewHolder1.class,this);
        new RecyclerUtil(mAdapter).set2View(recyclerView);
        List<String> debug = new ArrayList<>();
        debug.addAll(getDebugData());
        mAdapter.setData(debug);
    }

    private void simpleUse() {
        mAdapter = new BaseAdapter(this, R.layout.item_red) {
            @Override
            protected void onBindEasyHolder(BaseAdapter adapter, BaseViewHolder holder, int position,
                                            Object o) {
                holder.setItemText(R.id.txt, (String) o);
            }
        };
        new RecyclerUtil(mAdapter).set2View(recyclerView);
        List<String> debug = new ArrayList<>();
        debug.addAll(getDebugData());
        mAdapter.setData(debug);
    }


    private void showHeadViewHolder() {
        mAdapter.addOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(BaseAdapter adapter, BaseViewHolder holder, Object o, View view,
                                    int position) {
                LOG.e("MainActivity", position + ".onItemClick.77:" + o);
            }

            //            @Override
            //            public void onItemClick(BaseViewHolder holder, Object bean, View view, int position) {
            //                Log.e("MainActivity", "onItemClick:bean ->" + bean);
            //                Log.e("MainActivity", "onItemClick:holder ->" + holder);
            //                Log.e("MainActivity", "onItemClick:view ->" + view);
            //                Log.e("MainActivity", "onItemClick:position ->" + position);
            //            }
        });
        new RecyclerUtil(mAdapter).set2View(recyclerView);
    }

    public void click1(View view) {
        mAdapter.setDisplay(new BaseAdapter.DisplayOption<Object>() {
            @Override
            public boolean show(Object data, Object tag, int position) {
                if (tag instanceof String && data instanceof String) {
                    return ((String) data).contains((String) tag);
                }
                return true;
            }
        });
        mAdapter.display(null);
        new RecyclerUtil(mAdapter).set2View(recyclerView);
    }

    public void click2(View view) {
        mAdapter.setDisplay(new BaseAdapter.DisplayOption<Object>() {
            @Override
            public boolean show(Object data, Object tag, int position) {
                if (tag instanceof String) {
                    return position % 2 == 0;
                }
                return true;
            }
        });
        mAdapter.display("2");
        new RecyclerUtil(mAdapter).set2View(recyclerView);
    }

    public void click3(View view) {
        mAdapter.display(null);
        new RecyclerUtil(mAdapter).set2View(recyclerView);
    }

    public void click4(View view) {
        mAdapter.addFootHolder(6666, FootHolder.class, R.layout.item_green, MainActivity.this);
        mAdapter.addFootHolder(7777, FootHolder.class, R.layout.item_purple, MainActivity.this);
        mAdapter.addHeadHolder(1111, FootHolder.class, R.layout.item_green, MainActivity.this);
        mAdapter.addHeadHolder(2222, FootHolder.class, R.layout.item_purple, MainActivity.this);
        //    mAdapter
        //        .setHeadHolder(0, null, HeadViewHolder.class, R.layout.item_head_view, MainActivity.this);
    }

    public void click5(View view) {
        mAdapter.setEmpty(R.layout.item_empty);
        mAdapter.clearHeadView();
        mAdapter.clearFootView();
        mAdapter.clearData();
        mAdapter.setData(null);
        new RecyclerUtil(mAdapter).set2View(recyclerView);
    }

    public void click6(View view) {
        mAdapter.setData(getDebugData());
        new RecyclerUtil(mAdapter).set2View(recyclerView);
    }

    @Override
    public void onItemClick(BaseAdapter adapter, BaseViewHolder holder, Object o, View view,
                            int position) {
        LOG.e("MainActivity", "onItemClick.90:");
        if (holder instanceof HeadViewHolder) {
            if (view.getId() == R.id.txt_left) {
                LOG.e("MainActivity", "onItemClick.93:");
                Toast.makeText(MainActivity.this, "点击了左边->", Toast.LENGTH_SHORT).show();
            } else if (view.getId() == R.id.txt_right) {
                LOG.e("MainActivity", "onItemClick.96:");
                Toast.makeText(MainActivity.this, "点击了右边-> ", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private List getDebugData() {
        List debug = new ArrayList<>();
        debug.add(new MoreData("01     ##", 12));
        debug.add(new MoreData("02     ##", 12));
        debug.add(new MoreData("03     ##", 12));
        debug.add(new MoreData("04     ##", 12));
        debug.add(new MoreData("05     ##", 12));
        debug.add(new MoreData("06     ##", 12));
        debug.add(new MoreData("07     ##", 12));
        debug.add(new MoreData("08     ##", 12));
        debug.add(new MoreData("09     ##", 12));
        debug.add(new MoreData("10     ##", 12));
        debug.add(new MoreData("11     ##", 12));
        debug.add(new MoreData("12     ##", 12));
        debug.add(new MoreData("13     ##", 12));
        debug.add(new MoreData("14     ##", 12));
        debug.add(new MoreData("15     ##", 12));
        debug.add(new MoreData("16     ##", 12));
        return debug;
    }

}
